package com.pl.itsense.gwt.clientzone.ui;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.pl.itsense.gwt.clientzone.client.ClientZoneServiceAsync;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.MemberDTO;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler; 
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class ChangeDaneKontaktowePanel extends Window{
	static ChangeDaneKontaktowePanel instance = null;
	DynamicForm form = new DynamicForm();

	TextItem firstName = new TextItem();  
	TextItem lastName = new TextItem();  
	TextItem email = new TextItem();  
	TextItem phone = new TextItem();  
	TextItem companyName = new TextItem();  


	IButton okButton = new IButton("Zmień dane");
	IButton cancelButton = new IButton("Anuluj");
	
	
	RegExpValidator emailValidator = new RegExpValidator();

	public ChangeDaneKontaktowePanel() {
		instance = this;
		init();
	}
	
	
	private void init(){
		setWidth(350);  
		setHeight(260);
		setShowCloseButton(false);
		setTitle("Zmiana danych kontaktowych - ITSense Strefa Klienta");  
		setShowMinimizeButton(false);
		setIsModal(true);  
		setShowModalMask(true);  
		centerInPage();  
		setCanDragReposition(false);

		firstName.setName("firstName");
		firstName.setTitle("Imię");
		firstName.setRequired(false);
		firstName.setValue(Clientzone.memberDTO.getFirstName());
		
		lastName.setName("lastName");
		lastName.setTitle("Nazwisko");
		lastName.setRequired(false);
		lastName.setValue(Clientzone.memberDTO.getLastName());

		
		email.setName("email");
		email.setTitle("Email");  
		email.setRequired(true); 
		email.setValue(Clientzone.memberDTO.getEmail());

		  
		emailValidator.setErrorMessage("Podano błędny adres Email");  
		emailValidator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");  
		email.setValidators(emailValidator);  

		phone.setName("phone");
		phone.setTitle("Telefon");  
		phone.setRequired(false);
		phone.setValue(Clientzone.memberDTO.getPhone());

		
		
		
		companyName.setName("companyName");
		companyName.setTitle("Firma");  
		companyName.setRequired(false);  
		companyName.setWrapTitle(false);
		companyName.setValue(Clientzone.memberDTO.getCompany());
		

		form.setFields(firstName, lastName , email, companyName, phone);
	
		okButton.addClickHandler(new ClickHandler() {  
			public void onClick(ClickEvent event) {
				if (form.validate()){
					MemberDTO memberDTO = Clientzone.memberDTO;
					memberDTO.setFirstName(form.getValueAsString("firstName"));
					memberDTO.setCompany(form.getValueAsString("companyName"));
					memberDTO.setLastName(form.getValueAsString("lastName"));
					memberDTO.setEmail(form.getValueAsString("email"));
					memberDTO.setPhone(form.getValueAsString("phone"));
					Clientzone.clientZoneService.update(memberDTO, new AsyncCallback<MemberDTO>() {
						@Override
						public void onSuccess(MemberDTO result) {
							SC.say("Twoje dane kontaktowe zostały pomyślnie zmienione.", new BooleanCallback() {
								@Override
								public void execute(Boolean value) {
									instance.destroy();
									instance = null;
								}
							});
						}
						
						@Override
						public void onFailure(Throwable caught) {
							SC.say("Wystąpił błąd podczas modyfikacji danych kontaktowych", new BooleanCallback() {
								
								@Override
								public void execute(Boolean value) {
									instance.destroy();
									instance = null;
								}
							});
						}
					});
				}
			}
		});
		
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				instance.destroy();
				instance = null;
			}
		});

		form.setTop(20);
		form.setLeft(40);
		
		VLayout vLayout = new VLayout();
		vLayout.addChild(form);
		
		
		HLayout hLayout = new HLayout();
		hLayout.setHeight(30);
		hLayout.addMember(okButton);
		hLayout.addMember(cancelButton);		
		hLayout.setTop(180);
		hLayout.setLeft(65);
		hLayout.setMembersMargin(5);  
		vLayout.addChild(hLayout);
		
		addItem(vLayout);
		
	
	}
}
